package com.xzy.tool.str;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTool {
	
	/**
	 * 计算月份
	 * @param pattern yyyy-MM yyyyMM
	 * @param n 相差数1，-1...
	 * @return
	 */
	public static String calcMonth(String pattern,int n) 
	{ 
		  SimpleDateFormat format = new SimpleDateFormat(pattern);
		  Calendar cal=Calendar.getInstance();
		  cal.add(Calendar.MONTH, n);    //得到n个月 
		  long date = cal.getTimeInMillis();
          String d=format.format(date);
		  return d;
	} 
	/**
	 * 计算天数
	 * @param pattern yyyy-MM-dd yyyyMMdd
	 * @param n 相差数1，-1...
	 * @return
	 */
	public static String calcDate(String pattern,int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, n); // 得到n天
		String yestedayDate = new SimpleDateFormat(pattern).format(calendar
				.getTime());
		return yestedayDate;
	}
	/**
	 * 计算小时
	 * @param pattern yyyyMMddHH
	 * @param n 相差数1，-1...
	 * @return
	 */
	public static String calcHour(String pattern,int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR, n); 
		String yestedayDate = new SimpleDateFormat(pattern).format(calendar
				.getTime());
		return yestedayDate;
	}
	/**
	 * 计算分钟数
	 * @param pattern yyyyMMddHHmm
	 * @param n 相差数1，-1...
	 * @return
	 */
	public static String calcMinute(String pattern,int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, n); 
		String yestedayDate = new SimpleDateFormat(pattern).format(calendar
				.getTime());
		return yestedayDate;
	}
	
	/**
	 * 计算分钟数
	 * @param pattern yyyyMMddHHmm
	 * @param n 相差数1，-1...
	 * @return
	 */
	public static Date calcMinute(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, n);
		return calendar.getTime();
	}
	
	public static String getCurDate(String pattern) {
		Date date = new Date(System.currentTimeMillis());
		DateFormat simpleDate = new SimpleDateFormat(pattern);
		return simpleDate.format(date);
	}
	
	public static Date stringToDate(String source,String pattern) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		return sf.parse(source);
	}

	public static String dateToString(Date date,String pattern) {
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		return sf.format(date);
	}
	
/*	public static void main(String[] args) {
		System.out.println(calcMinute("yyyyMMddHHmm",10));
		System.out.println(calcHour("yyyyMMddHH",-1));
		System.out.println(calcDate("yyyyMMdd",1));
		System.out.println(calcMinute(10));
		System.out.println(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
	}*/

}
